#include <QLabel>
#include <QDockWidget>
#include <QFile>
#include <QTimer>

#include "CogsEngine.h"

#include "render/Renderer.h"
#include "render/RenderCommand.h"

namespace Cogs {
    static QString loadStyleSheet(const QString& fileName);

    CogsEngine::CogsEngine(QWidget* parent)
        : QMainWindow(parent)
    {
        

        ui.setupUi(this);
        
        m_DockManager = new ads::CDockManager(this);
        
        QMenuBar* menuBar = new QMenuBar(this);
        setMenuBar(menuBar);

        //添加"文件菜单"
        QMenu* fileMenu = menuBar->addMenu("文件");
        QAction* newAction = fileMenu->addAction("新建");
        QAction* openAction = fileMenu->addAction("打开");
        QAction* exitAction = fileMenu->addAction("退出");

        QMenu* windowMenu = menuBar->addMenu("窗口");
        QAction* view = windowMenu->addAction("视图");
        QAction* property = windowMenu->addAction("属性");
        QAction* textureAction = windowMenu->addAction("纹理");

        setWindowTitle("CogsEngin");
        resize(800, 600);

        m_ViewWindow = new ViewWindow(nullptr);
        ads::CDockWidget* docWidView = m_DockManager->createDockWidget("View", m_ViewWindow);
        m_DockManager->addDockWidget(ads::TopDockWidgetArea, docWidView);
        docWidView->setWidget(m_ViewWindow);

        m_ProPerWindow = new PropertyWindow(nullptr);
        ads::CDockWidget* docWidPro = m_DockManager->createDockWidget("Property", m_ProPerWindow);
        m_DockManager->addDockWidget(ads::TopDockWidgetArea, docWidPro);
        docWidPro->setWidget(m_ProPerWindow);

       

        RestoreDockLayout();

        connect(exitAction, &QAction::triggered, this, &QMainWindow::close);

        connect(view, &QAction::triggered, this, [=]() {
            docWidView->toggleView();
        });

        connect(property, &QAction::triggered, this, [=]() {
            docWidPro->toggleView();
        });

    }

    CogsEngine::~CogsEngine()
    {
    }

    void CogsEngine::closeEvent(QCloseEvent* event) {
        QFile fileAdsLayout("adsLayout.in");
        if (fileAdsLayout.open(QIODevice::WriteOnly)) {
            fileAdsLayout.write(m_DockManager->saveState().toBase64());
            fileAdsLayout.close();
        }

        QMainWindow::closeEvent(event);
    }

    void CogsEngine::RestoreDockLayout() {
        QFile fileAdsLayout("adsLayout.in");
        if (fileAdsLayout.open(QIODevice::ReadOnly)) {
            QByteArray base64 = fileAdsLayout.readAll();
            QByteArray state = QByteArray::fromBase64(base64);
            m_DockManager->restoreState(state);
            fileAdsLayout.close();
        }
    }

    QString loadStyleSheet(const QString& fileName)
    {
        QFile file(fileName);
        if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
        {
            qWarning() << "Cannot open stylesheet file:" << fileName;
            return QString();
        }

        QString styleSheet = QLatin1String(file.readAll());
        file.close();
        return styleSheet;
    }
}


